<template>
	<view class="login clearfix">
		<cu-custom></cu-custom>
		<form class="block clearfix  padding padding-bottom-lg radius-sm overflow-hidden  py20">
			<view class="flex  flex-direction login-box ">
				<view class="discenter"><image src="/static/login/logo.png" class="login-logo" mode=""></image></view>
				<view class="mt10 discenter f16 fwb">邻里</view>
				<view class="text-xxl px25 text-bold  py20">{{ i18n.title }}</view>
				<evan-form hide-required-asterisk ref="form" :model="form">
		 			<evan-form-item prop="phoneNum">
						<view class="cu-form-group h40 solid-bottom padding-lr-xs bb1" slot="formItem">
							<input type="number" :maxlength="11" placeholder-class="text-info" :placeholder="i18n.phone[0]" name="phoneNum" v-model.trim="form.phoneNum" />
						</view>
					</evan-form-item>
					<evan-form-item prop="password">
						<view class="cu-form-group h40 solid-bottom padding-lr-xs bb1" slot="formItem">
							<input password  :maxlength="16" placeholder-class="text-info" :placeholder="i18n.password[0][0]" name="password" v-model.trim="form.password" />
						</view>
					</evan-form-item>
				</evan-form>
				<button :disabled="isLoading" @click="formValidate" type class="mt20    button-red text-white">
					<text v-if="isLoading" class="cuIcon-loading2 cuIconfont-spin margin-right-sm"></text>
					{{ i18n.title }}
				</button>
				<view class=" mt10 flex align-center justify-between f12">
					<view class="flex align-center text-danger" @click="pushPage('/pages/login/resetPassword')">忘记密码?></view>
					
					<view class="forget padding-tb-xs text-danger" @click="pushPage('/pages/login/register')">
						<text class="text-info">没有账号？</text>去注册></view>
				</view>
				<!-- v-if="status=='0'" -->
				<view class="margin-lr-xs discenter mt20 " >
					<view class="login-line mr15"></view>
					<view class="flex align-center text-info">使用其他账号登录</view>
					<view class="login-line ml15"></view>
				</view>
				<view class="  margin-lr-xs discenter mt20"  @click="weixinlogin">
					<image src="/static/login/wx.png" class="block60"></image>
				</view>
			</view>
		</form>
		<view class="login-end discenter" @click="pushPage('/pages/login/xieyi')">
			<text>登录/注册即表示同意</text>
			<text class="text-red">《用户协议》</text>
		</view>
	</view>
</template>

<script>
import { mapState, mapActions } from 'vuex'
export default {
	data() {
		return {
			form: {
				phoneNum: '',
				password: ''
			},
			weixinid:'',
			status:1,
			systeam:'android'
		}
	},
	computed: {
		rules() {
			return {
				 phoneNum: [{ required: true, message: this.i18n.phone[1] }, { pattern: this.$pattern.isMobile, message: this.i18n.phone[2] }],
				 password: [{ required: true, message: this.i18n.password[1][0] }]
			}
		}
	},
	onLoad() {},
	onShow() {
		this.getBanben()
	},
	methods: {
		...mapActions(['setToken']),
		async formSubmit() {
			// this.$Router.pushTab('/pages/home/index')
			let { data } = await this.$http.post('login.login', this.form, { isAuth: false, tipConfig: { storeLoading: true, isSuccessTip: true } })
			this.setToken(data.token)
			setTimeout(() => {
				this.getUser()
				this.$Router.pushTab('/pages/home/index')
			}, 800)
		},
		getBanben(){
			uni.getSystemInfo({
				success: res => {
					if(res.platform=='ios'){
						this.systeam='ios'
						this.status='0'
						this.wxstatus()
					}else{
						this.systeam='android'
						this.status='0'
					}
				}
			})
		},
		async wxstatus() {
			let {
				data: { status }
			} = await this.$http.post('login.upgrade', {}, { isAuth: false, tipConfig: { isLoading: false, isErrorDefaultTip: false } })
			this.status=status
		},
		async weixintologin(){
			let { data } = await this.$http.post('login.wxlogin', {code:this.weixinid}, { isAuth: false, tipConfig: { storeLoading: true, isSuccessTip: true } })
			// if(data.type==0){
			// 	this.pushPage('/pages/login/register?weixin='+this.weixinid)
			// }else{
			// 	this.setToken(data.token)
			// 	setTimeout(() => {
			// 		this.getUser()
			// 		this.$Router.pushTab('/pages/home/index')
			// 	}, 800)
			// }
			this.setToken(data.token)
			setTimeout(() => {
				this.getUser()
				this.$Router.pushTab('/pages/home/index')
			}, 800)
		},
		weixinlogin(){
			let _this = this;
			uni.login({
			    provider: 'weixin',
			    success: function (loginRes) {
				    uni.getUserInfo({
					  provider: 'weixin',
					  success: function (infoRes) {
						//console.log('用户昵称为：' + infoRes.userInfo.nickName);
						//console.log('用户ID：' + infoRes.userInfo.openId);
						//console.log('用户头像：' + infoRes.userInfo.avatarUrl);
						console.log(_this.weixinid,infoRes)
						_this.weixinid=infoRes.userInfo.openId
						_this.weixintologin()
					  }
					});
			    },
				fail: function (loginRes) {
			       console.log(loginRes.authResult);
			    },
			});
		}
	}
}
</script>

<style lang="less">
	.login-logo{
		width: 125rpx;
		height: 125rpx;
	}
	.login-box{
		width: 100%;
		margin: auto;
		
		position: relative;
		z-index: 1;
		padding: 40rpx 0rpx;
	}
	.login-line{
		width: 135rpx;
		height: 2rpx;
		background-color: #ECECEC;
	}
</style>
